package com.x.mvp.widget.dateselector.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.x.mvp.R;
import com.lsp.RulerView;

/**
 * Created by H on 18/4/18.
 */

public class RulerDialog extends BaseDialog {
    private View dialogView;
    private RulerView rulerView;
    private TextView resault;
    String ret;
    int max;
    int min;

    private OnClickListener onClickListener;

    public RulerDialog(Context mContext) {
        this.context = mContext;
        create();
    }

    private void create() {

        if (dialog != null) {
            return;
        }
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        dialogView = layoutInflater.inflate(R.layout.dialog_ruler, null);
        rulerView = (RulerView) dialogView.findViewById(R.id.ruler);
        resault = (TextView) dialogView.findViewById(R.id.resault);
        if (max > 0) {
            rulerView.setFirstScale((max + min) / 2);
            ret = (max + min) / 2 + "";
            rulerView.setMinScale(min);
            rulerView.setMaxScale(max);
        }
        rulerView.setOnChooseResulterListener(new RulerView.OnChooseResulterListener() {
            @Override
            public void onEndResult(String s) {
                ret = s;
                resault.setText(s + "");
            }

            @Override
            public void onScrollResult(String s) {
                resault.setText(s + "");
            }
        });
        View cancelBt = dialogView.findViewById(R.id.cannel);
        cancelBt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    if (!onClickListener.onCancel()) {
                        dialog.dismiss();
                    }
                } else {
                    dialog.dismiss();
                }
            }
        });
        View sureBt = dialogView.findViewById(R.id.ok);
        sureBt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    if (!onClickListener.onSure(ret)) {
                        dialog.dismiss();
                    }
                } else {
                    dialog.dismiss();
                }
            }
        });
        dialog = new Dialog(context, com.x.mvp.R.style.quick_dialog);
        initDialog();
        dialog.setContentView(dialogView);
    }

    public void show(int max, int min) {
        if (dialog == null || dialog.isShowing()) {
            return;
        }
        if (rulerView != null) {
            rulerView.setFirstScale((max + min) / 2);
            ret = (max + min) / 2 + "";
            rulerView.setMinScale(min);
            rulerView.setMaxScale(max);
        } else {
            this.max = max;
            this.min = min;
            ret = (max + min) / 2 + "";
        }
        dialog.show();
    }


    public void setOnClickListener(OnClickListener listener) {
        onClickListener = listener;
    }

    public interface OnClickListener {
        boolean onSure(String s);

        boolean onCancel();
    }
}
